﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class student_js : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["name"].ToString();
        Label2.Text = DateTime.Today.ToShortDateString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    string Constr = "Server=(local);Integrated Security=true;DataBase=tsmanager;";//采用windows身份验证方式
    protected void Button1_Click1(object sender, EventArgs e)
    {
        foreach (GridViewRow dgi in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)dgi.FindControl("CheckBox1");
            if (cb.Checked == true)
            {
                SqlConnection con = new SqlConnection(Constr);
                con.Open();
                string strcom = "delete from JHbooks where ReadersID='" + dgi.Cells[0].Text.Trim() + "' and BI_BID='" + dgi.Cells[1].Text.Trim() + "'";
                SqlCommand com = new SqlCommand(strcom, con);
                try
                {
                    com.ExecuteNonQuery();
                    selectdata(dgi.Cells[1].Text.Trim());
                    updata(dgi.Cells[1].Text.Trim());
                    
                    Response.Write("<script language=javascript>alert('还书成功！')</script>");
                }
                catch (Exception ex)
                {
                    Response.Write("<script language=javascript>alert('还书失败！')</script>");
                    Response.Write(dgi.Cells[0].Text.Trim() + dgi.Cells[1].Text.Trim());
                    Response.Write(ex);
                }
            }
        }
    }

    string bookkc;
    string bookjc;
    protected void selectdata(string bookid)
    {
        SqlConnection con = new SqlConnection(Constr);
        con.Open();
        string strcom = "select * from BookInformation where BI_BID='" + bookid + "'";
        SqlCommand com = new SqlCommand(strcom, con);
        try
        {
            SqlDataReader sdr = com.ExecuteReader();
            if (sdr.Read())
            {
                bookkc = sdr["BI_NOB"].ToString();
            }
            else
            {
                Response.Write("<script language=javascript>alert('查询失败！')</script>");
            }

        }
        catch (Exception ex)
        {

        }

    }

    protected void updata(string bookid)
    {
        SqlConnection con = new SqlConnection(Constr);
        con.Open();
        bookkc = Convert.ToString(Convert.ToInt32(bookkc) + 1);
        string strcom = "update BookInformation set BI_NOB='" + bookkc + "' where BI_BID='" + bookid + "'";
        SqlCommand com = new SqlCommand(strcom, con);
        try
        {
            com.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('更新成功！')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            Response.Write("<script language=javascript>alert('更新失败！')</script>");
        }

    }
}